//-----------------------------------------------------------------------
// <copyright file="FeedbackSummaryPresenter.cs" company="Simnova LLC">
//     Copyright (c) Simnova LLC. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace Simnova.Collaborate.Presenter
{
    using System;
    using System.Collections;
    using System.Text;
    using Simnova.Collaborate.View;

    public class FeedbackSummaryPresenter
    {
        private IFeedbackSummary feedbackSummaryView;
        private int feedbackId;
        private int userId;

        public FeedbackSummaryPresenter(IFeedbackSummary feedbackSummaryView, int feedbackId, int userId)
        {
            this.feedbackSummaryView = feedbackSummaryView;
            this.feedbackId = feedbackId;
            this.userId = userId;

            AppServer.CollaborateServiceClient collaborateClient = new AppServer.CollaborateServiceClient();
            AppServer.Feedback feedback = collaborateClient.GetFeedback(this.feedbackId);
            collaborateClient.Close();
            this.feedbackSummaryView.Title = feedback.Title;
            this.feedbackSummaryView.Description = feedback.Description;
            this.feedbackSummaryView.CreateDate = feedback.CreateDate;
            this.feedbackSummaryView.Ratings = feedback.Ratings;
            this.feedbackSummaryView.AverageRating = feedback.AverageRating;
            this.feedbackSummaryView.Workarounds = feedback.Workarounds;
            this.LoadWatchingFeedback();
            this.LoadComment();
        }

        public void SetWatchingFeedback()
        {
            AppServer.CollaborateServiceClient collaborateClient = new AppServer.CollaborateServiceClient();
            AppServer.FeedbackWatchList feedbackWatchList = collaborateClient.GetFeedbackWatchListByFeedback(this.userId, this.feedbackId);
            bool alreadyWatching = (feedbackWatchList != null);
            if (alreadyWatching == false)
            {
                feedbackWatchList = new AppServer.FeedbackWatchList();
                feedbackWatchList.UserId = this.userId;
                feedbackWatchList.FeedbackId = this.feedbackId;
                collaborateClient.SaveFeedbackWatchList(feedbackWatchList);
            }
            else
            {
                collaborateClient.DeleteFeedbackWatchList(feedbackWatchList);
            }

            collaborateClient.Close();
            this.LoadWatchingFeedback();
        }

        public void SaveComment()
        {
            AppServer.CollaborateServiceClient collaborateClient = new AppServer.CollaborateServiceClient();
            
            AppServer.FeedbackComment feedbackComment = collaborateClient.GetFeedbackCommentByFeedback(this.userId, this.feedbackId);
            if (feedbackComment == null)
            {
                feedbackComment = new AppServer.FeedbackComment();
                feedbackComment.UserId = this.userId;
                feedbackComment.FeedbackId = this.feedbackId;
            }

            feedbackComment.Comment = this.feedbackSummaryView.Comment;
            collaborateClient.SaveFeedbackComment(feedbackComment);
            collaborateClient.Close();
            this.LoadComment();
        }

        private void LoadComment()
        {
            AppServer.CollaborateServiceClient collaborateClient = new AppServer.CollaborateServiceClient();
            AppServer.FeedbackComment feedbackComment = collaborateClient.GetFeedbackCommentByFeedback(this.userId, this.feedbackId);
            if (feedbackComment == null)
            {
                this.feedbackSummaryView.Comment = "";
            }
            else
            {
                this.feedbackSummaryView.Comment = feedbackComment.Comment;
            }

            IList feedbackComments = collaborateClient.GetFeedbackComments(this.feedbackId);
            this.feedbackSummaryView.Comments = feedbackComments;

            collaborateClient.Close();
        }

        private void LoadWatchingFeedback()
        {
            AppServer.CollaborateServiceClient collaborateClient = new AppServer.CollaborateServiceClient();
            AppServer.FeedbackWatchList feedbackWatchList = collaborateClient.GetFeedbackWatchListByFeedback(this.userId, this.feedbackId);
            collaborateClient.Close();
            this.feedbackSummaryView.WatchingFeedback = (feedbackWatchList != null);
        }
    }
}
